<template>
  <Panel>
    <template #title>
      <el-form ref="ruleForm" :model="queryData" label-width="80px" size="mini">
        <el-form-item label="订单号" prop="orderNo">
          <el-input
            v-model="queryData.orderNo"
            placeholder="请填写订单号"
          ></el-input>
        </el-form-item>
        <el-form-item label="收货人" prop="consignee">
          <el-input
            v-model="queryData.consignee"
            placeholder="请填写收货人"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号" placeholder="请填写手机号" prop="phone">
          <el-input v-model="queryData.phone"></el-input>
        </el-form-item>

        <el-form-item label="订单状态" prop="orderState">
          <el-select
            v-model="queryData.orderState"
            placeholder="请选择订单状态"
          >
            <el-option label="已受理" value="已受理"></el-option>
            <el-option label="派送中" value="派送中"></el-option>
            <el-option label="已完成" value="已完成"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择时间">
          <el-date-picker
            v-model="queryData.data"
            type="datetimerange"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            size="mini"
            v-waves
            type="primary"
            @click="getOrderslist('search')"
            >查询</el-button
          >
          <!-- 下载表格 -->
          <Btn />
          <el-button v-waves size="mini" @click="resetForm('ruleForm')"
            >查询所有订单</el-button
          >
        </el-form-item>
      </el-form>
    </template>
    <template #content>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column fixed prop="orderNo" label="订单号" width="100">
        </el-table-column>
        <el-table-column label="下单时间" width="160">
          <template v-slot="{ row }">
            {{ row.orderTime | dataForat }}
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="手机号" width="120">
        </el-table-column>
        <el-table-column prop="consignee" label="收货人" width="120">
        </el-table-column>
        <el-table-column prop="deliverAddress" label="配送地址" width="150">
          <template v-slot="{ row }">
            <el-popover trigger="hover" placement="top">
              <p>配送地址: {{ row.deliverAddress }}</p>
              <div slot="reference" class="name-wrapper">
                {{ row.deliverAddress }}
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="送达时间" width="160">
          <template v-slot="{ row }">
            {{ row.deliveryTime | dataForat }}
          </template>
        </el-table-column>
        <el-table-column prop="remarks" label="用户备注" width="120">
        </el-table-column>
        <el-table-column prop="orderAmount" label="订单金额" width="120">
        </el-table-column>
        <el-table-column prop="orderState" label="订单状态" width="120">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template v-slot="{ row }">
            <el-button
              v-waves
              @click="handleClick(row)"
              type="text"
              size="small"
              >查看</el-button
            >
            <el-button
              v-waves
              type="text"
              size="small"
              @click="$router.push(`/orders/edit?id=${row.id}`)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <Paging :pageData="paging" @pagingIncident="getOrderslist" />
      <!-- 提示框 -->
      <el-dialog title="订单信息" width="30%" :visible.sync="dialogVisible">
        <p>
          订单号: <span>{{ lookData.orderNo }}</span>
        </p>
        <p>
          下单时间: <span>{{ lookData.orderTime | dataForat }}</span>
        </p>
        <p>
          联系电话: <span> {{ lookData.phone }}</span>
        </p>
        <p>
          收货人: <span>{{ lookData.consignee }}</span>
        </p>
        <p>
          送货地址: <span>{{ lookData.deliverAddress }}</span>
        </p>
        <p>
          送达时间: <span>{{ lookData.deliveryTime | dataForat }}</span>
        </p>
        <p>
          备注: <span>{{ lookData.remarks }}</span>
        </p>
        <p>
          订单金额: <span>{{ lookData.orderAmount }}</span>
        </p>
        <el-steps
          simple
          finish-status="success"
          :active="
            lookData.orderState === '已受理'
              ? 1
              : lookData.orderState === '派送中'
              ? 2
              : 3
          "
        >
          <el-step title="已受理" icon="el-icon-edit"></el-step>
          <el-step title="配送中" icon="el-icon-truck"></el-step>
          <el-step title="已完成" icon="el-icon-tableware"></el-step>
        </el-steps>
        <span slot="footer" class="dialog-footer">
          <el-button @click="$router.push(`/orders/edit?id=${lookData.id}`)"
            >去编辑</el-button
          >
          <el-button type="primary" @click="dialogVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </template>
  </Panel>
</template>

<script>
import { orderList, orderSearch } from '@/api/orders'
import Btn from '@/components/Excelexport'

export default {
  components: {
    Btn
  },
  data() {
    return {
      queryData: {
        orderNo: '',
        consignee: '',
        phone: '',
        orderState: '',
        date: []
      },
      paging: {
        currentPage: 1,
        pageSize: 4,
        total: 0
      },
      tableData: [],
      lookData: {},
      dialogVisible: false
    }
  },
  created() {
    this.getOrderslist()
  },
  mounted() {},

  methods: {
    /* 获取订单列表 */
    async getOrderslist(val) {
      const orderData = { ...this.queryData, ...this.paging }
      orderData.date = JSON.stringify(orderData.data)
      let res = null
      if (val === 'search') {
        this.paging.currentPage = 1
        res = await orderSearch(orderData)
      } else {
        res = await orderList(orderData)
      }
      this.paging.total = res.total
      this.tableData = res.data
    },
    handleClick(row) {
      this.lookData = row
      this.dialogVisible = true
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.getOrderslist()
    }
  }
}
</script>

<style lang="less" scoped>
.el-form {
  display: flex;
  flex-wrap: wrap;
}
.el-dialog {
  width: 800px;
  p {
    line-height: 30px;
    font-size: 20px;
    letter-spacing: 2px;
    color: #000;
    span {
      letter-spacing: normal;
      margin-left: 4px;
      color: #666;
    }
  }
  .el-steps {
    margin-top: 26px;
    /deep/.is-process {
      font-weight: normal;
    }
  }
}
.name-wrapper {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
